﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Bus_proj.website
{
    public partial class sell_ticket_page2 : System.Web.UI.Page
    {
        int chargePerSheet = 100, row, col;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {
                        BindSeats();
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }          
        }
        void BindSeats()
        {   
            TextBox4.Text = chargePerSheet.ToString();            
            TextBox_date.Text = DateTime.Now.ToLongDateString();
            int session_around = Int32.Parse(Session["data_AroundID"].ToString());
            string sel = @"select Seat_no
                            from Table_Sale
                            INNER JOIN Table_SSID
                                ON  Table_Sale.SSID = Table_SSID.SSID
                            INNER JOIN Table_SaleDetail
                                ON  Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                            where Table_Sale.Around_id = " + session_around + " and Table_Sale.SSID = 1";
            string selbooking = @"select Seat_no
                            from Table_Sale
                            INNER JOIN Table_SSID
                                ON  Table_Sale.SSID = Table_SSID.SSID
                            INNER JOIN Table_SaleDetail
                                ON  Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                            where Table_Sale.Around_id = " + session_around + " and Table_Sale.SSID = 2";
            string search = @"select Table_Around.Route_id , Table_Type_Bus.Type_Bus_seat_row , Table_Type_Bus.Type_Bus_seat_colum 
                            from Table_Around
                            INNER JOIN Table_Point
	                            ON	Table_Around.Route_id = Table_Point.Route_id
                            INNER JOIN Table_Type_Bus
	                            ON	Table_Around.Type_Bus_id = Table_Type_Bus.Type_Bus_id
                            where Table_Around.Around_id = " + session_around + "";
            ///////////////////
            string show_detil = @"select Table_Around.Around_no,Table_Around.Around_date_chk,Table_Time.Time_time,Table_Route.Route_source,
		                        Table_Route.Route_destination,Bus_Line_id
                                from Table_Around
                                inner join Table_Route
                                on Table_Route.Route_id = Table_Around.Route_id
                                inner join Table_Time
                                on Table_Around.Time_id = Table_Time.Time_id
                                where Table_Around.Around_id= " + session_around + "";
            ///////////////
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sel, sqlconn);
            SqlDataAdapter da2 = new SqlDataAdapter(selbooking, sqlconn);
            SqlDataAdapter da3 = new SqlDataAdapter(search, sqlconn);
            //////////
            SqlDataAdapter da_show_detil = new SqlDataAdapter(show_detil, sqlconn);
            //////
            DataTable dt = new DataTable();
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            /////////
            DataTable dt_show_detil = new DataTable();
            //////
            da.Fill(dt);
            da2.Fill(dt2);
            da3.Fill(dt3);
            /////////
            da_show_detil.Fill(dt_show_detil);
            //////
            Session["S_Route_id"] = Int32.Parse(dt3.Rows[0]["Route_id"].ToString());
            row = Int32.Parse(dt3.Rows[0]["Type_Bus_seat_row"].ToString());
            col = Int32.Parse(dt3.Rows[0]["Type_Bus_seat_colum"].ToString());
            ///////////
            if (dt_show_detil.Rows.Count > 0)
            {
                Label15.Text = dt_show_detil.Rows[0]["Around_no"].ToString();
                Label16.Text = dt_show_detil.Rows[0]["Route_source"].ToString() + "-" + dt_show_detil.Rows[0]["Route_destination"].ToString();
                DateTime chk_show = DateTime.Parse(dt_show_detil.Rows[0]["Around_date_chk"].ToString());
                string str_show = String.Format("{0:'วัน'dddd'ที่' d MMMM gg yyyy}", chk_show);
                Label17.Text = str_show;
                Label18.Text = dt_show_detil.Rows[0]["Time_time"].ToString();
                Label19.Text = dt_show_detil.Rows[0]["Bus_Line_id"].ToString();
            }
            ////////
            string seatid = "";
            string seatbookingid = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i >= 1)
                {
                    seatid = seatid + "," + dt.Rows[i]["Seat_no"].ToString();
                }
                else
                {
                    seatid = dt.Rows[i]["Seat_no"].ToString();
                }
            }
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                if (i >= 1)
                {
                    seatbookingid = seatbookingid + "," + dt2.Rows[i]["Seat_no"].ToString();
                }
                else
                {
                    seatbookingid = dt2.Rows[i]["Seat_no"].ToString();
                }
            }
            if (seatid != "")
            {
                string[] stemp = seatid.Split(',');
                int count = stemp.Length;
                int check = 0;
                for (int i = 0; i < count; i++)
                {
                    if (stemp[i].Substring(1, 1) == "A")
                    {
                        string[] stempb = stemp[i].Split('A');
                        check = row - Int32.Parse(stempb[0].ToString());
                        check = row - check;
                        if (i >= 1)
                        {
                            seatid = seatid + "," + check.ToString();
                        }
                        else
                        {
                            seatid = check.ToString();
                        }
                    }
                    else if (stemp[i].Substring(1, 1) == "B")
                    {
                        string[] stempb = stemp[i].Split('B');
                        check = row - Int32.Parse(stempb[0].ToString());
                        check = row - check;
                        check = row + check;
                        if (i >= 1)
                        {
                            seatid = seatid + "," + check.ToString();
                        }
                        else
                        {
                            seatid = check.ToString();
                        }
                    }
                    else if (stemp[i].Substring(1, 1) == "C")
                    {
                        string[] stempb = stemp[i].Split('C');
                        check = row - Int32.Parse(stempb[0].ToString());
                        check = row - check;
                        check = row * 2 + check;
                        if (i >= 1)
                        {
                            seatid = seatid + "," + check.ToString();
                        }
                        else
                        {
                            seatid = check.ToString();
                        }
                    }
                    else if (stemp[i].Substring(1, 1) == "D")
                    {
                        string[] stempb = stemp[i].Split('D');
                        check = row - Int32.Parse(stempb[0].ToString());
                        check = row - check;
                        check = row * 3 + check;
                        if (i >= 1)
                        {
                            seatid = seatid + "," + check.ToString();
                        }
                        else
                        {
                            seatid = check.ToString();
                        }
                    }
                }
            }
            if (seatbookingid != "")
            {
                string[] stemp2 = seatbookingid.Split(',');
                int count2 = stemp2.Length;
                int check2 = 0;
                for (int i = 0; i < count2; i++)
                {
                    if (stemp2[i].Substring(1, 1) == "A")
                    {
                        string[] stempb = stemp2[i].Split('A');
                        check2 = row - Int32.Parse(stempb[0].ToString());
                        check2 = row - check2;
                        if (i >= 1)
                        {
                            seatbookingid = seatbookingid + "," + check2.ToString();
                        }
                        else
                        {
                            seatbookingid = check2.ToString();
                        }
                    }
                    else if (stemp2[i].Substring(1, 1) == "B")
                    {
                        string[] stempb = stemp2[i].Split('B');
                        check2 = row - Int32.Parse(stempb[0].ToString());
                        check2 = row - check2;
                        check2 = row + check2;
                        if (i >= 1)
                        {
                            seatbookingid = seatbookingid + "," + check2.ToString();
                        }
                        else
                        {
                            seatbookingid = check2.ToString();
                        }
                    }
                    else if (stemp2[i].Substring(1, 1) == "C")
                    {
                        string[] stempb = stemp2[i].Split('C');
                        check2 = row - Int32.Parse(stempb[0].ToString());
                        check2 = row - check2;
                        check2 = row * 2 + check2;
                        if (i >= 1)
                        {
                            seatbookingid = seatbookingid + "," + check2.ToString();
                        }
                        else
                        {
                            seatbookingid = check2.ToString();
                        }
                    }
                    else if (stemp2[i].Substring(1, 1) == "D")
                    {
                        string[] stempb = stemp2[i].Split('D');
                        check2 = row - Int32.Parse(stempb[0].ToString());
                        check2 = row - check2;
                        check2 = row * 3 + check2;
                        if (i >= 1)
                        {
                            seatbookingid = seatbookingid + "," + check2.ToString();
                        }
                        else
                        {
                            seatbookingid = check2.ToString();
                        }
                    }
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "Init", "init([" + seatid + "],[" + seatbookingid + "],[" + row + "],[" + col + "]);", true);
        }

        protected void Bt_save_Click(object sender, EventArgs e)
        {
            Session["name_customers"] = TextBox2.Text;
            Session["phone_customers"] = TextBox3.Text;            
            Session["seat_id_customers"] = Request.Form[TextBox5.UniqueID];
            Session["point_customers"] = DropDownList1.SelectedValue;
            Response.Redirect("sell_ticket2_sum.aspx");          
        }
    }
}